/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sd.chat;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

/**
 *
 * @author Old
 */
public class OSReceiver extends Thread {
    
    
    private ObjectInputStream ois;
    private receiveControler myManager;
    private InputStream is;
    private Logger log = new Logger(true);
    private boolean mustRun = true;

    public OSReceiver(receiveControler myManager, InputStream is) {
        this.myManager = myManager;
        this.is = is;
    }
    
    @Override
    public void run() {
        while(mustRun){
            try {
                ois = new ObjectInputStream(is);
                TCPMsg data = (TCPMsg) ois.readObject();
                log.outln("OSReceiver received message: "+data);
                myManager.addReceivedMessage(data);
            } catch (IOException ex) {
                myManager.unexpectedShutdown();
                this.mustRun = false;
                log.erro("OSReceiver IOException:"+ ex);
            } catch (ClassNotFoundException ex) {
                log.erro("OSReceiver ClassNotFoundException:"+ ex);
            }
            
        }
        log.outln("OSReceiver terminating...");
    }

    public boolean isMustRun() {
        return mustRun;
    }

    public void setMustRun(boolean mustRun) {
        this.mustRun = mustRun;
    }
    
    
}
